from django.http import HttpResponse
import json

def hello(request):
    return HttpResponse("Hello world ! ")


# For request path field
def welcome(request, par):
    msg = 'Welcome to python 3, ' + par
    return HttpResponse(msg)


# For request attribute
def user(request):
    if request.method == 'GET':
        print(request.path)
        user_id = request.GET.get('user_id', '002')
        print(user_id)
        if user_id == '001':
            user_obj = {
                'user_id': user_id,
                'username': 'admin'
            }
            return HttpResponse(json.dumps(user_obj), content_type="application/json")
    else:
        return HttpResponse('bad request ... ')


# For request body
# @api_view(['POST'])
def user_profile(request):
    print('method', request.method)
    if request.method == 'POST':
        username = request.POST.get('username')
        print('username: ', username)
        password = request.POST.get('password')
        print('password: ', password)
        if username == 'admin' and password == '*':
            profile = {
                'username': username,
                'nickname': 'YY'
            }
            return HttpResponse(json.dumps(profile), content_type="application/json")
    else:
        return HttpResponse('Authenticated failed')

